package com.guo.core.framework.tomcatStudy;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @projectName: 接口梳理
 * @package: com.guo.core.framework.tomcatStudy
 * @className: ServletProcessor1
 * @author: zhangcan30
 * @description: TODO
 * @date: 2023/6/25 17:27
 * @version: 1.0
 */
public class ServletProcessor1 {

    public void process(Request request, Response response) {
        String uri = request.getUri();
        String servletName = uri.substring(uri.lastIndexOf("/") + 1);
        URLClassLoader loader = null;


        try {
            URL[] urls = new URL[1];
            URLStreamHandler streamHandler = null;
            File classpath = new File("");
            String repository = (new URL("file", null, classpath.getCanonicalPath() + File.separator)).toString();
            urls[0]=new URL(null,repository,streamHandler);
            loader=new URLClassLoader(urls);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Class myClass=null;
        try {
            myClass = loader.loadClass(servletName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Servlet servlet=null;

        try {
            servlet= (Servlet) myClass.newInstance();
            servlet.service((ServletRequest) request,(ServletResponse) response);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(null);

        Optional<Integer> min = list.stream().min(Comparator.comparing(o -> o));
        if (min.isPresent()){
            System.out.println(min.get());
        }

    }
}
